//
//  SetSign.m
//  tcpoctest
//
//  Created by gump on 12/31/12.
//  Copyright (c) 2012 gump. All rights reserved.
//

#import "SetSign.h"
#import "taskDataSource.h"

#define PROMPT_NUMBER 50
@implementation SetSign

-(id)init
{
    self = [super init];
    if(self)
    {
        inputTF = nil;
        mai = nil;
    }
    
    return self;
}

-(void)initUI
{
    [self createTitle];
    self.mtitle.text = @"修改签名";
    
    UIBarButtonItem *backItem = [self createBarBackButtonItemWithTitle:@"返回" target:self action:@selector(backButtonPressed)];
    self.navItem.leftBarButtonItem = backItem;
    
    UIBarButtonItem *modifyItem = [self createBarButtonItemWithTitle:@"提交" target:self action:@selector(modifyButtonPressed)];
    self.navItem.rightBarButtonItem = modifyItem;
    
    //个性签名底图
    UIImageView *inputBgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 55, 300, 100)];
    inputBgView.image = [UIImage imageNamed:@"cell.png"];
    inputBgView.userInteractionEnabled = YES;
    [self.view addSubview:inputBgView];
    
    //个性签名输入框
    inputTF = [[UIPlaceHolderTextView alloc]initWithFrame:CGRectMake(5, 5, 290, 90)];
    inputTF.font = CENTER_CELL_CONTENT_FONT;
    inputTF.textColor = [UIColor colorWithWhite:0.4 alpha:1.0];
    inputTF.font = [UIFont systemFontOfSize:18.0];
    inputTF.placeholder = @"请输入个性签名";
    inputTF.placeholderTextColor = [UIColor colorWithWhite:0.7 alpha:1.0];
    inputTF.delegate = self;
    [inputBgView addSubview:inputTF];
    
    
    //提示剩余字数的label
    UILabel * promptLabel = [[UILabel alloc] initWithFrame:CGRectMake(285, 160, 30, 25)];
    promptLabel.backgroundColor = [UIColor clearColor];
    promptLabel.textColor = [UIColor grayColor];
    promptLabel.tag = 101;
    [self.view addSubview:promptLabel];
    
}

#pragma mark - 每次界面将要出现时调用
-(void)viewWillAppear:(BOOL)animated{
    if ([[DataControl shareControl].personDS.uinfo.strSign isEqualToString:@""]) {
        inputTF.placeholder = @"请输入个性签名";//个性签名数据为空就给此提示
    }else{
        inputTF.text = [DataControl shareControl].personDS.uinfo.strSign;//读取的个性签名的值
    }
    
    UILabel * promptLabel = (UILabel *)[self.view viewWithTag:101];
    promptLabel.text = [NSString stringWithFormat:@"%d",PROMPT_NUMBER-inputTF.text.length];//根据当前解析出的字数来确定提示字数是多少
    
    [inputTF becomeFirstResponder];//textview为第一个响应者
}


#pragma mark -private fun-
-(void)backButtonPressed
{
    [[ViewManager sharedViewManager] backView:self];//返回上一界面
    [inputTF resignFirstResponder];
}

//提交按钮
-(void)modifyButtonPressed
{
    if ([inputTF.text length] > 0 && [inputTF.text length] <= PROMPT_NUMBER)
    {
        [inputTF resignFirstResponder];
        [[DataControl shareControl] SetChangeSign:inputTF.text];
        [DataControl shareControl].personDS.uinfo.strSign = inputTF.text;
        
        [[DataControl shareControl] SetUpdateUserType:UUT_SIGN];
        [[DataControl shareControl].LCDS addLogicCommand:LC_UPDATEUIDB];
        //update personview
        [[ViewManager sharedViewManager] PICVsetLabelText];
        [[ViewManager sharedViewManager] backView:self];
        
        [[DataControl shareControl].LCDS addLogicCommand:LC_SETSIGN];
        
        [[DataControl shareControl].taskDS CompleteTaskByID:TI_F_CHANGESIGN
                                           withIsDirectShow:true];
    }else if ([inputTF.text length] > PROMPT_NUMBER){
        //当大于50个字提示不能发送
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"签名超过50个字，不允许发送" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
        [alert show];
    }
    
}

#pragma mark - textview
//点击空白处回收键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [inputTF resignFirstResponder];
}

//每次改变text的都调用
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([textView.text isEqualToString:@"请输入个性签名"]) {
        textView.text = @"";
    }
    
    if ([textView.text length] > (PROMPT_NUMBER + 10) && text.length > 0)
    {
        return NO;//超出50个字符不能输入
    }
    
    return YES;
}

-(void)textViewDidChange:(UITextView *)textView{
    UILabel * promptLabel = (UILabel *)[self.view viewWithTag:101];
    promptLabel.text = [NSString stringWithFormat:@"%d",PROMPT_NUMBER-[self sinaCountWord:textView.text]];
    
    if(PROMPT_NUMBER - [self sinaCountWord:textView.text] < 0) {
        promptLabel.textColor = [UIColor redColor];//输入大于50个字字体变为红色
    }else{
        promptLabel.textColor = [UIColor grayColor];//输入不大于50个字字体为灰色
    }
    
    
}

#pragma mark--字数统计
- (int)sinaCountWord:(NSString*)s
{
    
    int i,n=[s length],l=0,a=0,b=0;
    
    unichar c;
    
    for(i=0;i<n;i++){
        
        c=[s characterAtIndex:i];
        
        if(isblank(c)){
            
            b++;
            
        }else if(isascii(c)){
            
            a++;
            
        }else{
            
            l++;
            
        }
        
    }
    
    if(a==0 && l==0) return 0;
    
    return l+(int)ceilf((float)(a+b));
    
}



@end





